package textdiff;

public class StringReportWriter implements IReportWriter {
	private String lineNumPad = "    "; // Line numbers will be padded to this

	private String result = "";

	public void report(EditCommand command) {
		result += (command.command + " ");
		print(command.oldLines, "Old");
		print(command.newLines, "New");
		result += (" \n");
	}

	private void print(LineBlock lineBlock, String fileDescription) {
		if (null != lineBlock) {
			result += (fileDescription + " line(s) " + (lineBlock.fromLineNum + 1) + "-" + (lineBlock.thruLineNum + 1) + " \n");
			if (lineBlock.reportable) {
				int lineNum = lineBlock.fromLineNum + 1;
				for (int i = 0; i < lineBlock.lines.length; i++) {
					result += (pad(lineNum++) + ": " + lineBlock.lines[i] + "\n");
				}
			}
		}
	}

	private String pad(int lineNum) {
		String paddedNum = "" + lineNum;
		if (paddedNum.length() < lineNumPad.length())
			return (lineNumPad + paddedNum).substring(paddedNum.length());
		else
			return paddedNum;
	}

	public String getResult() {
		return result;
	}
}
